package com.openlogic.common.plugins.spi.enums;

import java.util.Arrays;
import java.util.NoSuchElementException;

/**
 * @ClassName: DbType
 * @Description: 数据库枚举类型
 * @author weiyongpeng
 * @date 2025年1月23日 下午8:52:27
 */
public enum DbType {

	/**
	 * MYSQL
	 */
	MYSQL("1", "MySql数据库",
			"jdbc:mysql://${host}:${port}/${dbName}?serverTimezone=GMT%2B8&characterEncoding=UTF-8&useUnicode=true&useSSL=false"),
	/**
	 * MARIADB
	 */
	MARIADB("2", "MariaDB数据库", "jdbc:mariadb://${host}:${port}/${dbName}"),
	/**
	 * ORACLE
	 */
	ORACLE("3", "Oracle11g及以下数据库", "jdbc:oracle:thin:@${host}:${port}:${sid}"),
	/**
	 * oracle12c new pagination
	 */
	ORACLE_12C("4", "Oracle12c+数据库", "jdbc:oracle:thin:@${host}:${port}:${sid}"),
	/**
	 * POSTGRESQL
	 */
	POSTGRE_SQL("5", "PostgreSql数据库", "jdbc:postgresql://${host}:${port}/${dbName}"),
	/**
	 * SQLSERVER2005
	 */
	SQL_SERVER2008("6", "SQLServer2008及以下数据库", "jdbc:sqlserver://${host}:${port};DatabaseName=${dbName}"),
	/**
	 * SQLSERVER
	 */
	SQL_SERVER("7", "SQLServer2012+数据库", "jdbc:sqlserver://${host}:${port};DatabaseName=${dbName}"),
	/**
	 * EXCEL 数据源
	 */
	EXCEL("8", "EXCEL数据库", ""),
	/**
	 * API 数据源
	 */
	API("9", "API数据库", ""),
	/**
	 * UNKONWN DB
	 */
	OTHER("10", "其他数据库", "");

	/**
	 * 数据库名称
	 */
	private final String db;

	/**
	 * 描述
	 */
	private final String desc;

	/**
	 * url
	 */
	private final String url;

	public String getDb() {
		return this.db;
	}

	public String getDesc() {
		return this.desc;
	}

	public String getUrl() {
		return this.url;
	}

	DbType(String db, String desc, String url) {
		this.db = db;
		this.desc = desc;
		this.url = url;
	}

	/**
	 * 获取数据库类型
	 *
	 * @param dbType 数据库类型字符串
	 */
	public static DbType getDbType(String dbType) {
		for (DbType type : DbType.values()) {
			if (type.db.equals(dbType)) {
				return type;
			}
		}
		return OTHER;
	}

	public static DbType ofName(String name) {
		return Arrays.stream(DbType.values()).filter(e -> e.name().equals(name)).findFirst()
				.orElseThrow(() -> new NoSuchElementException("no such db type"));
	}

}
